#include "Camera.h"

// CoreLib
#include "MathConstants.h"
#include "MathHelpers.h"

// GLM
#include <gtc/matrix_transform.hpp>

// std
#include <sstream>


le::Camera::Camera()
: m_Position(glm::vec3(0.f, 0.f, -5.f))
, m_Right(glm::vec3(1.f, 0.f, 0.f))
, m_Lookat(glm::vec3(0.f, 0.f, 1.f))
, m_Up(glm::vec3(0.f, 1.f, 0.f))
, m_ViewMatrix(GetIdentityMat4())
, m_ProjectionMatrix(GetIdentityMat4())
, m_FOV(PI / 2)
, m_NearPlane(0.001f)
, m_FarPlane(1000.f)
, m_AspectRatio(4.f / 3.f)
{
	UpdateViewMatrix();
	UpdateProjectionMatrix();
}

le::Camera::~Camera()
{

}

void le::Camera::UpdateViewMatrix()
{
	//m_Position = glm::vec3(0.f, 0.f, -5.f);
	m_ViewMatrix = glm::lookAt(m_Position, m_Lookat, m_Up);
}

void le::Camera::UpdateProjectionMatrix()
{
	m_ProjectionMatrix =  glm::perspective(90.f, 1.f, 0.1f, 100.f);
	/*m_ProjectionMatrix = mat44::IDENTITY;

	float d2r = M_PI / 180.0f;
	float yScale = 1.0f / tan(d2r * m_FOV / 2.f);
	float xScale = yScale / m_AspectRatio;
	float nearmfar = m_NearPlane - m_FarPlane;

	m_ProjectionMatrix.SetRow(0, vec4(xScale, 0.f, 0.f, 0.f));
	m_ProjectionMatrix.SetRow(1, vec4(0.f, yScale, 0.f, 0.f));
	m_ProjectionMatrix.SetRow(2, vec4(0.f, 0.f, (m_NearPlane + m_FarPlane) / nearmfar, -1.f));
	m_ProjectionMatrix.SetRow(3, vec4(0.f, 0.f, (2.f * m_FarPlane * m_NearPlane) / nearmfar, 0.f));*/
	

}


const glm::mat4& le::Camera::GetProjectionMatrix() const
{
	return m_ProjectionMatrix;
}